import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DatabaseConnection{
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/student_management";
        String user = "root";
        String password = "";

        try (Connection connection = DriverManager.getConnection(url, user, password)) {
            // 连接成功
            System.out.println("连接成功");
            String sql = "INSERT INTO User (id, name) VALUES (?, ?)";
            try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
                preparedStatement.setInt(1, 1);
                preparedStatement.setString(2, "Milko");
                int rowsAffected = preparedStatement.executeUpdate();
                System.out.println("影响行数: " + rowsAffected);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
